//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support AJAX");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var suggestReq = getXmlHttpRequestObject();
//Our XmlHttpRequest object to get the auto suggest
var readReq = getXmlHttpRequestObject();
//state of the suggestion list
var isOpen = false;
//total number od addresses suggested
var numberOfAddr = 0;
//the string of the addresses suggested, separated by "; "
var addrList = "";


//get the list of suggested addresses
function addrSuggest() {
	if (suggestReq.readyState == 4 || suggestReq.readyState == 0) {
		var str = escape(document.getElementById('destination').value);
		var ss = document.getElementById('addr_suggest');
		if (!(isOpen)){
			isOpen = 'true';
			ss.style.display = "block";
			ss.style.visibility = "visible";
			ss.innerHTML = '<i> Loading ... </i>';
			suggestReq.open("GET", '/suggestAddr', true);
			suggestReq.onreadystatechange = handleAddrSuggest; 
			suggestReq.send(null);
		}else{
			ss.style.display = "none";
			ss.innerHTML = '';
			isOpen = false;
		}
	}		
}

//build and display the list of suggested addresses
function buildAddrList(){
	var s = document.getElementById('addr_suggest');
	s.innerHTML = '';
	var alreadySelected = document.getElementById('destination').value.split("; ");
	var str = addrList.split("\n");
	var suggest = '<div onmouseover="javascript:allOver(this);" ';
	suggest += 'onmouseout="javascript:allOut(this);" ';
	suggest += 'onclick="javascript:setAll();" ';
	suggest += 'class="suggest_all_link"><strong><i> -- All -- </i></strong></div>';
	s.innerHTML += suggest;
	var isNotSelected = "false";
	numberOfAddr = str.length;
	for(i=0; i < str.length ; i++) {
		isNotSelected = "true";
		for(j=0; j < alreadySelected.length ; j++) {
			if (str[i] == alreadySelected[j]){
				isNotSelected = "false";
				//break;
			}
		}
		if (isNotSelected == "true"){
			suggest = '<div id="item'+i+'" onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setAddr(this);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			s.innerHTML += suggest;
		}else{
			suggest = '<div id="item'+i+'" onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setAddr(this);" ';
			suggest += 'class="suggest_link_strong">' + str[i] + '</div>';
			s.innerHTML += suggest;
		}
	}
}

//Called when the AJAX response is returned.
function handleAddrSuggest() {
	if (suggestReq.readyState == 4) {
		addrList = suggestReq.responseText;
		var ss = document.getElementById('addr_suggest')
		ss.style.display = "block";
		ss.style.visibility = "visible";
		if (addrList == ''){
			//display error
			ss.innerHTML = '<i> didn\'t work... double click to try again ! </i>';
		}else{
			if (addrList == 'nothing_at_all'){
				//no names returned, blank result
				addrList = '';
			}
			buildAddrList();
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	if (div_value.className == 'suggest_link_strong'){
		div_value.className = 'suggest_link_over_strong'
	}else{
		div_value.className = 'suggest_link_over'
	}
}
//Mouse out function
function suggestOut(div_value) {
	if (div_value.className == 'suggest_link_over_strong'){
		div_value.className = 'suggest_link_strong'
	}else{
		div_value.className = 'suggest_link'
	}
}
//Click function
function setAddr(suggItem) {
	var s = document.getElementById('destination');
	if (suggItem.className == 'suggest_link_over'){
		suggItem.className = 'suggest_link_over_strong'
		s.value += suggItem.innerHTML+'; ';
	}else{
		suggItem.className = 'suggest_link_over'
		str = s.value.split("; ");
		res = "";
		for (i=0; i< str.length; i++){
			if (str[i] != suggItem.innerHTML && str[i] != ""){
				res += str[i]+'; ';
			}
		}
		s.value = res;
	}
}

//All Mouse over function
function allOver(div_value) {
	div_value.className = 'suggest_all_link_over';
}
//All Mouse out function
function allOut(div_value) {
	div_value.className = 'suggest_all_link';
}
//All Click function
function setAll() {
	var s = document.getElementById('destination');
	var allAddrSelected = "true";
	for(i=0; i < numberOfAddr ; i++) {
		item = document.getElementById('item'+i);
		if (item.className == 'suggest_link'){
			allAddrSelected = "false";
			break;
		}
	}
	var item;
	if (allAddrSelected == "false"){
		s.value = '';
		for(i=0; i < numberOfAddr ; i++) {
			item = document.getElementById('item'+i);
			item.className = 'suggest_link_strong';
			allAddrSelected = "true";
			s.value += item.innerHTML+'; ';
		}
	}else{
		for(i=0; i < numberOfAddr ; i++) {
			item = document.getElementById('item'+i);
			item.className = 'suggest_link';
			allAddrSelected = "false";
			s.value = '';
		}
	}
}

//hide suggestion when address text input selected
function hideSuggest() {
	isOpen = false;
	var s = document.getElementById('addr_suggest');
	s.innerHTML = '';
	s.style.display = "none";
}

var last_input = '';
//load a message page
function goToMessage(index, e) {
	var unicode = e.keyCode? e.keyCode : e.charCode;
	var p = document.getElementById('gtp_input');
	if (unicode == '13'){
		var r = new RegExp("^[0-9]*$");
		if(r.test(index)){
			//value entered is numeric only
			index --;
			if (index < 0){
				p.value = '1';
			}else{
				window.location.href="/messageBox/"+(index * msgPerPage);
			}
		}
	}
}

////for displayed messages
function handleNotifyReadAck(){
	if (readReq.readyState == 4) {
		var reply = readReq.responseText;
		if (reply != ''){
			//change display of message now it is not new anymore
			var replyList = reply.split(";");
			var h = document.getElementById('message_header_'+replyList[1])
			var b = document.getElementById('message_content_'+replyList[1])
			if(h.className == 'header_new'){
				h.className = 'header';
				b.className = 'content';
			}else{
				h.className = 'header_collapsed';
				b.className = 'content_collapsed';
			}
		}
	}
}

//click
function headerClick(id) {
	var p = document.getElementById('message_content_'+id);
	var d = document.getElementById('message_header_'+id);
	if(d.className == 'header'){
		d.className = 'header_collapsed';
		p.className = 'content_collapsed';
	}else if(d.className == 'header_collapsed'){
		d.className = 'header';
		p.className = 'content';
	}else if(d.className == 'header_new'){
		d.className = 'header_collapsed_new';
		p.className = 'content_collapsed';
	}else if(d.className == 'header_collapsed_new'){
		d.className = 'header_new';
		p.className = 'content_new';
		if (readReq.readyState == 4 || readReq.readyState == 0) {
			//notify server that message has been opened
			readReq.open("GET", '/messageBox/notifyRead/'+id, true);
			readReq.onreadystatechange = handleNotifyReadAck; 
			readReq.send(null);
		}
	}else
	
	{
		if (d.className == 'header_new'){
			if (readReq.readyState == 4 || readReq.readyState == 0) {
				//notify server that message has been opened
				readReq.open("GET", '/messageBox/notifyRead/'+id, true);
				//suggestReq.onreadystatechange = handleAddrSuggest; 
				readtReq.send(null);
			}
		}
		d.className = 'header';
		p.className = 'content';
	}
}

var msg_id_list = [];

function expandCollapseAll(button) {
	if (button.innerHTML == "expand"){
		button.innerHTML = "collapse";
		for(var i=0; i<msg_id_list.length; i++){
			var p = document.getElementById('message_content_'+msg_id_list[i]);
			var h = document.getElementById('message_header_'+msg_id_list[i]);
			if (h.className != "header_collapsed_new" && h.className != "header_new"){
				h.className = 'header';
				p.className = 'content';
			}
		}
	}else{
		button.innerHTML = "expand";
		for(var i=0; i<msg_id_list.length; i++){
			var p = document.getElementById('message_content_'+msg_id_list[i]);
			var h = document.getElementById('message_header_'+msg_id_list[i]);
			if (h.className != "header_collapsed_new" && h.className != "header_new"){
				h.className = 'header_collapsed';
				p.className = 'content_collapsed';
			}
		}
	}
}
